/* Copyright 2008 Bas van den Berg
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "yaffut.h"
#include "myassert.h"

#define private public
#include "PlayerSettings.h"
#undef private

#include "StringBuilder.h"
#include "Error.h"
#include "color.h"

using namespace std;
using namespace Chronos;

struct PlayerSettingsTest {
    PlayerSettingsTest() {
        settings.add("descr", "brief", "long");
        settings.add("setting1", "option1", "option2");
        settings.add("setting2", "option3", "option4");
    }
    PlayerSettings settings;
    StringBuilder buffer;
};


TEST(PlayerSettingsTest, testPrintAllOptions) {
    settings.print(buffer);
    ASSERT_STR_EQUAL("settings:\n"
                     "descr     <"ANSI_WHITE"brief"ANSI_NORMAL",long>\n"
                     "setting1  <"ANSI_WHITE"option1"ANSI_NORMAL",option2>\n"
                     "setting2  <"ANSI_WHITE"option3"ANSI_NORMAL",option4>\n", buffer);
}


TEST(PlayerSettingsTest, testSettingOption) {
    settings.setOption(buffer, "descr", "long");
    ASSERT_STR_EQUAL("", buffer);
    ASSERT_STR_EQUAL("long", settings.getOption("descr"));

    settings.setOption(buffer, "descr", "brief");
    ASSERT_STR_EQUAL("", buffer);
    ASSERT_STR_EQUAL("brief", settings.getOption("descr"));
}


TEST(PlayerSettingsTest, testSettingUnknownSetting) {
    settings.setOption(buffer, "unknown", "option");
    ASSERT_STR_EQUAL("Unknown setting. Type 'settings' for a list of settings\n", buffer);
}


TEST(PlayerSettingsTest, testSettingUnknownOption) {
    settings.setOption(buffer, "setting1", "option6");
    ASSERT_STR_EQUAL("Unknown option. Valid options are <"ANSI_WHITE"option1"ANSI_NORMAL",option2>\n", buffer);
}


TEST(PlayerSettingsTest, testGettingOption) {
    ASSERT_STR_EQUAL("brief", settings.getOption("descr"));
}


TEST(PlayerSettingsTest, testGettingUnknownSetting) {
    try {
        settings.getOption("unknown");
        ASSERT_FAIL();
    } catch (MudError& e) {
        ASSERT_STR_EQUAL("Unknown setting: unknown", e.what());
    }
}


TEST(PlayerSettingsTest, testSerializing) {
    ASSERT_STR_EQUAL("descr\nbrief\nsetting1\noption1\nsetting2\noption3\n", settings.toString());

    settings.setOption(buffer, "descr", "long");
    settings.setOption(buffer, "setting2", "option4");
    ASSERT_STR_EQUAL("descr\nlong\nsetting1\noption1\nsetting2\noption4\n", settings.toString());
}


TEST(PlayerSettingsTest, testDeSerializing) {
    string input = "descr\nlong\nsetting1\noption1\nsetting2\noption4\n";
    settings.initialize(input);
    ASSERT_STR_EQUAL("descr\nlong\nsetting1\noption1\nsetting2\noption4\n", settings.toString());
}

